from flask import Flask, render_template, request, jsonify

# 加载规则
frequent\_itemsets = pd.read\_pickle('./frequent\_itemsets.pkl')
rules = pd.read\_pickle('./rules.pkl')
app = web.Flask(__name__)

@app.route('/recommend', methods=['POST'])
def recommend():
    data = request.json.get('items', [])
    # 生成关联规则推荐
    recommendationDatas = []
    for i, rule in rules.iterrows():
        antecedents = list(rule['antecedents'])
        consequents = list(rule['consequents'])
        # 检查当前页面是否有输入匹配
        if set(antecedents).issubset(set(data)):
            recommendationDatas.append(data)
            recommendationDatas.extend(consequents)
        # 去重并返回结果
        recommendationDatas = list(set(recommendationDatas))
        # 规则推荐结果
        return jsonify({'recommendations': recommendationDatas})

if __name__ == '__main__':
    app.run(debug=True)
